package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Students;
import org.example.service.IStudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/students")
public class StudentsController {

    @Autowired
    IStudentsService studentsService;
    @GetMapping("/a1List")
    public List<Students> getStuList(){
        System.out.println("getList");
        return studentsService.list();
    }
    @PostMapping("/search")
    public Map<String, Object> search(@RequestBody Map<String, String> req){
        System.out.println("search=========>"+req);
        Map<String, Object> map = studentsService.listByReq(req);
        return map;


//        Page<Students> page = new Page<>((Integer) req.get("currentPage4"), (Integer) req.get("pageSize"));



//        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
//        if(req.get("studentID") != null){
//            queryWrapper.eq("studentID", req.get("studentID"));
//        }
//        Page<Students> stud = studentsService.page(page,queryWrapper);
//        System.out.println(stud);
//        return stud;
//        return studentsService.listByMap(req);
    }
    @PostMapping("/addStu")
    public boolean addStu(@RequestBody Map<String, String> stu){
        System.out.println("addStu"+stu);
        Students stu1 = new Students();
        stu1.setPhoneNumber(stu.get("PhoneNumber"));
        stu1.setFirstName(stu.get("FirstName"));
        stu1.setLastName(stu.get("LastName"));
        stu1.setDateOfBirth(stu.get("DateOfBirth"));
        stu1.setGender(stu.get("Gender"));
        stu1.setEmail(stu.get("Email"));
        stu1.setAddress(stu.get("Address"));
        stu1.setCity(stu.get("City"));
        stu1.setState(stu.get("State"));
        stu1.setCountry(stu.get("Country"));
        return studentsService.save(stu1);
    }



    @PostMapping("/remov")
    public Map<String, Object> remov(@RequestBody Map<String, String> req){
        System.out.println("------req-login-->"+req);
        Students students = new Students();
        students.setStudentID(Integer.valueOf(req.get("StudentID")));
        boolean res = studentsService.removeById(students);
        Map<String, Object> map = new HashMap<>();
        if(res){
            map.put("success", "删除成功");
            return map;
        }
        map.put("errorMsg", "删除失败");
        return map;
    }
    @PostMapping("/update")
    public Map<String, Object> update(@RequestBody Students req){
        System.out.println("------req-login-->"+req);
        boolean res = studentsService.updateById(req);
        Map<String, Object> map = new HashMap<>();
        if(res){
            map.put("success", "修改成功");
            return map;
        }
        map.put("errorMsg", "修改失败");
        return map;
    }
}
